﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Kiiro.Common.Interfaces;
using Kiiro.Common.Utility;

namespace Kiiro.Common.Entities
{
    [Serializable]
    public class User : BaseEntity, IEntity
    {
        #region Private Properties

        private int spUserId;
        private List<ValidationMessage> spUserIdMessage = new List<ValidationMessage> ( );
        private string login;
        private List<ValidationMessage> loginMessage = new List<ValidationMessage> ( );
        private string password;
        private List<ValidationMessage> passwordMessage = new List<ValidationMessage> ( );
        private string passwordSecurityQuestion;
        private List<ValidationMessage> passwordSecurityQuestionMessage = new List<ValidationMessage> ( );
        private string passwordSecurityAnswer;
        private List<ValidationMessage> passwordSecurityAnswerMessage = new List<ValidationMessage> ( );
        private string firstName;
        private List<ValidationMessage> firstNameMessage = new List<ValidationMessage> ( );
        private string lastName;
        private List<ValidationMessage> lastNameMessage = new List<ValidationMessage> ( );
        private string phoneNumber;
        private List<ValidationMessage> phoneNumberMessage = new List<ValidationMessage> ( );
        private string email;
        private List<ValidationMessage> emailMessage = new List<ValidationMessage> ( );
        private string im;
        private List<ValidationMessage> imMessage = new List<ValidationMessage> ( );
        private string imClient;
        private List<ValidationMessage> imClientMessage = new List<ValidationMessage> ( );
        private string pictureUrl;
        private List<ValidationMessage> pictureUrlMessage = new List<ValidationMessage> ( );
        private byte [ ] pictureBytes;
        private List<ValidationMessage> pictureBytesMessage = new List<ValidationMessage> ( );
        private string statusNote;
        private List<ValidationMessage> statusNoteMessage = new List<ValidationMessage> ( );
        private bool isActive;
        private List<ValidationMessage> isActiveMessage = new List<ValidationMessage> ( );
        private bool isDeleted;
        private List<ValidationMessage> isDeletedMessage = new List<ValidationMessage> ( );
        private bool isAdministrator;
        private List<ValidationMessage> isAdministratorMessage = new List<ValidationMessage> ( );
        private bool isFormsUser;
        private List<ValidationMessage> isFormsUserMessage = new List<ValidationMessage> ( );
        private bool receiveEmailNotification;
        private List<ValidationMessage> receiveEmailNotificationMessage = new List<ValidationMessage> ( );
        private DateTime lastLoginTime;
        private List<ValidationMessage> lastLoginTimeMessage = new List<ValidationMessage> ( );
        private List<int> projectIds = new List<int> ( );
        private List<ValidationMessage> projectIdsMessage = new List<ValidationMessage> ( );
        private bool isSharePointAccount;
        private List<ValidationMessage> isSharePointAccountMessage = new List<ValidationMessage> ( );
        private bool addToAllProjects;
        private List<ValidationMessage> addToAllProjectsMessage = new List<ValidationMessage> ( );
        private string companyName;
        private List<ValidationMessage> companyNameMessage = new List<ValidationMessage> ( );
        private bool manageProjectsOnly;
        private List<ValidationMessage> manageProjectsOnlyMessage = new List<ValidationMessage> ( );

        //private bool isADGroup;
        private List<ValidationMessage> isADGroupMessage = new List<ValidationMessage> ( );
        //private bool isExplicitMember;
        private List<ValidationMessage> isNonExplicitMemberMessage = new List<ValidationMessage> ( );

        private List<KiiroGroup> groups = new List<KiiroGroup> ( );

        #endregion

        #region Public Properties

        public int SPUserId
        {
            get { return spUserId; }
            set
            {
                if ( spUserId != value )
                {
                    spUserId = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> SPUserIdMessage
        {
            get { return spUserIdMessage; }
            set
            {
                if ( spUserIdMessage != value )
                {
                    spUserIdMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string Login
        {
            get { return login; }
            set
            {
                if ( login != value )
                {
                    login = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> LoginMessage
        {
            get { return loginMessage; }
            set
            {
                if ( loginMessage != value )
                {
                    loginMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                if ( password != value )
                {
                    password = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> PasswordMessage
        {
            get { return passwordMessage; }
            set
            {
                if ( passwordMessage != value )
                {
                    passwordMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string PasswordSecurityQuestion
        {
            get { return passwordSecurityQuestion; }
            set
            {
                if ( passwordSecurityQuestion != value )
                {
                    passwordSecurityQuestion = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> PasswordSecurityQuestionMessage
        {
            get { return passwordSecurityQuestionMessage; }
            set
            {
                if ( passwordSecurityQuestionMessage != value )
                {
                    passwordSecurityQuestionMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string PasswordSecurityAnswer
        {
            get { return passwordSecurityAnswer; }
            set
            {
                if ( passwordSecurityAnswer != value )
                {
                    passwordSecurityAnswer = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> PasswordSecurityAnswerMessage
        {
            get { return passwordSecurityAnswerMessage; }
            set
            {
                if ( passwordSecurityAnswerMessage != value )
                {
                    passwordSecurityAnswerMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string FirstName
        {
            get { return firstName; }
            set
            {
                if ( firstName != value )
                {
                    firstName = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> FirstNameMessage
        {
            get { return firstNameMessage; }
            set
            {
                if ( firstNameMessage != value )
                {
                    firstNameMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string LastName
        {
            get { return lastName; }
            set
            {
                if ( lastName != value )
                {
                    lastName = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> LastNameMessage
        {
            get { return lastNameMessage; }
            set
            {
                if ( lastNameMessage != value )
                {
                    lastNameMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string PhoneNumber
        {
            get { return phoneNumber; }
            set
            {
                if ( phoneNumber != value )
                {
                    phoneNumber = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> PhoneNumberMessage
        {
            get { return phoneNumberMessage; }
            set
            {
                if ( phoneNumberMessage != value )
                {
                    phoneNumberMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string Email
        {
            get { return email; }
            set
            {
                if ( email != value )
                {
                    email = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> EmailMessage
        {
            get { return emailMessage; }
            set
            {
                if ( emailMessage != value )
                {
                    emailMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string IM
        {
            get { return im; }
            set
            {
                if ( im != value )
                {
                    im = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> IMMessage
        {
            get { return imMessage; }
            set
            {
                if ( imMessage != value )
                {
                    imMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string IMClient
        {
            get { return imClient; }
            set
            {
                if ( imClient != value )
                {
                    imClient = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> IMClientMessage
        {
            get { return imClientMessage; }
            set
            {
                if ( imClientMessage != value )
                {
                    imClientMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string PictureUrl
        {
            get { return pictureUrl; }
            set
            {
                if ( pictureUrl != value )
                {
                    pictureUrl = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> PictureUrlMessage
        {
            get { return pictureUrlMessage; }
            set
            {
                if ( pictureUrlMessage != value )
                {
                    pictureUrlMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public byte [ ] PictureBytes
        {
            get { return pictureBytes; }
            set
            {
                if ( pictureBytes != value )
                {
                    pictureBytes = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> PictureBytesMessage
        {
            get { return pictureBytesMessage; }
            set
            {
                if ( pictureBytesMessage != value )
                {
                    pictureBytesMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string StatusNote
        {
            get { return statusNote; }
            set
            {
                if ( statusNote != value )
                {
                    statusNote = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> StatusNoteMessage
        {
            get { return statusNoteMessage; }
            set
            {
                if ( statusNoteMessage != value )
                {
                    statusNoteMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if ( isActive != value )
                {
                    isActive = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> IsActiveMessage
        {
            get { return isActiveMessage; }
            set
            {
                if ( isActiveMessage != value )
                {
                    isActiveMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public bool IsDeleted
        {
            get { return isDeleted; }
            set
            {
                if ( isDeleted != value )
                {
                    isDeleted = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> IsDeletedMessage
        {
            get { return isDeletedMessage; }
            set
            {
                if ( isDeletedMessage != value )
                {
                    isDeletedMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public bool IsFormsUser
        {
            get { return isFormsUser; }
            set
            {
                if ( isFormsUser != value )
                {
                    isFormsUser = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> IsFormsUserMessage
        {
            get { return isFormsUserMessage; }
            set
            {
                if ( isFormsUserMessage != value )
                {
                    isFormsUserMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public bool IsAdministrator
        {
            get { return isAdministrator; }
            set
            {
                if ( isAdministrator != value )
                {
                    isAdministrator = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> IsAdministratorMessage
        {
            get { return isAdministratorMessage; }
            set
            {
                if ( isAdministratorMessage != value )
                {
                    isAdministratorMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public bool ReceiveEmailNotification
        {
            get { return receiveEmailNotification; }
            set
            {
                if ( receiveEmailNotification != value )
                {
                    receiveEmailNotification = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> ReceiveEmailNotificationMessage
        {
            get { return receiveEmailNotificationMessage; }
            set
            {
                if ( receiveEmailNotificationMessage != value )
                {
                    receiveEmailNotificationMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public DateTime LastLoginTime
        {
            get { return lastLoginTime; }
            set
            {
                if ( lastLoginTime != value )
                {
                    lastLoginTime = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> LastLoginTimeMessage
        {
            get { return lastLoginTimeMessage; }
            set
            {
                if ( lastLoginTimeMessage != value )
                {
                    lastLoginTimeMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public List<int> ProjectIds
        {
            get { return projectIds; }
            set
            {
                if ( projectIds != value )
                {
                    projectIds = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> ProjectIdsMessage
        {
            get { return projectIdsMessage; }
            set
            {
                if ( projectIdsMessage != value )
                {
                    projectIdsMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public bool IsSharePointAccount
        {
            get { return this.isSharePointAccount; }
            set
            {
                if ( this.isSharePointAccount != value )
                {
                    this.isSharePointAccount = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> IsSharePointAccountMessage
        {
            get { return this.isSharePointAccountMessage; }
            set
            {
                if ( this.isSharePointAccountMessage != value )
                {
                    this.isSharePointAccountMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public bool AddToAllProjects
        {
            get { return this.addToAllProjects; }
            set
            {
                if ( this.addToAllProjects != value )
                {
                    this.addToAllProjects = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> AddToAllProjectsMessage
        {
            get { return this.addToAllProjectsMessage; }
            set
            {
                if ( this.addToAllProjectsMessage != value )
                {
                    this.addToAllProjectsMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string CompanyName
        {
            get { return companyName; }
            set
            {
                if ( companyName != value )
                {
                    companyName = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> CompanyNameMessage
        {
            get { return companyNameMessage; }
            set
            {
                if ( companyNameMessage != value )
                {
                    companyNameMessage = value;
                    UpdateEntity ( );
                }
            }
        }


        public bool ManageProjectsOnly
        {
            get { return this.manageProjectsOnly; }
            set
            {
                if ( this.manageProjectsOnly != value )
                {
                    this.manageProjectsOnly = value;
                    UpdateEntity ( );
                }
            }
        }
        public List<ValidationMessage> ManageProjectsOnlyMessage
        {
            get { return this.manageProjectsOnlyMessage; }
            set
            {
                if ( this.manageProjectsOnlyMessage != value )
                {
                    this.manageProjectsOnlyMessage = value;
                    UpdateEntity ( );
                }
            }
        }

        public string FullName
        {
            get { return FirstName + " " + LastName; }
        }

        public bool IsADGroup
        {
            get;
            set;
        }

        public List<ValidationMessage> IsADGroupMessage
        {
            get { return isADGroupMessage; }
            set { isADGroupMessage = value; }
        }

        public bool IsNonExplicitMember
        {
            get;
            set;
        }

        public List<ValidationMessage> IsNonExplicitMemberMessage
        {
            get { return isNonExplicitMemberMessage; }
            set { isNonExplicitMemberMessage = value; }
        }

        public List<KiiroGroup> Groups
        {
            get;
            set;
        }

        #endregion

        #region IEntity Members
        public new bool IsValid ( )
        {
            return AllValidationErrors ( ).Count == 0;
        }

        public new List<ValidationMessage> AllValidationErrors ( )
        {
            List<ValidationMessage> allErrors = new List<ValidationMessage> ( );

            allErrors.AddRange ( AddToAllProjectsMessage );
            allErrors.AddRange ( EditTypeMessage );
            allErrors.AddRange ( EmailMessage );
            allErrors.AddRange ( FirstNameMessage );
            allErrors.AddRange ( IMClientMessage );
            allErrors.AddRange ( IMMessage );
            allErrors.AddRange ( IsActiveMessage );
            allErrors.AddRange ( IsADGroupMessage );
            allErrors.AddRange ( IsAdministratorMessage );
            allErrors.AddRange ( IsFormsUserMessage );
            allErrors.AddRange ( IsNonExplicitMemberMessage );
            allErrors.AddRange ( IsSharePointAccountMessage );
            allErrors.AddRange ( LastLoginTimeMessage );
            allErrors.AddRange ( LastNameMessage );
            allErrors.AddRange ( LoginMessage );
            allErrors.AddRange ( ManageProjectsOnlyMessage );
            allErrors.AddRange ( PasswordMessage );
            allErrors.AddRange ( PasswordSecurityAnswerMessage );
            allErrors.AddRange ( PasswordSecurityQuestionMessage );
            allErrors.AddRange ( PhoneNumberMessage );
            allErrors.AddRange ( PictureBytesMessage );
            allErrors.AddRange ( PictureUrlMessage );
            allErrors.AddRange ( ProjectIdsMessage );
            allErrors.AddRange ( ReceiveEmailNotificationMessage );
            allErrors.AddRange ( StatusNoteMessage );
            allErrors.AddRange ( TitleMessage );
            
            return allErrors;
        }

        public new void ClearValidationMessages ( )
        {
            AddToAllProjectsMessage.Clear ( );
            EditTypeMessage.Clear ( );
            EmailMessage.Clear ( );
            FirstNameMessage.Clear ( );
            IMClientMessage.Clear ( );
            IMMessage.Clear ( );
            IsActiveMessage.Clear ( );
            IsADGroupMessage.Clear ( );
            IsAdministratorMessage.Clear ( );
            IsFormsUserMessage.Clear ( );
            IsNonExplicitMemberMessage.Clear ( );
            IsSharePointAccountMessage.Clear ( );
            LastLoginTimeMessage.Clear ( );
            LastNameMessage.Clear ( );
            LoginMessage.Clear ( );
            ManageProjectsOnlyMessage.Clear ( );
            PasswordMessage.Clear ( );
            PasswordSecurityAnswerMessage.Clear ( );
            PasswordSecurityQuestionMessage.Clear ( );
            PhoneNumberMessage.Clear ( );
            PictureBytesMessage.Clear ( );
            PictureUrlMessage.Clear ( );
            ProjectIdsMessage.Clear ( );
            ReceiveEmailNotificationMessage.Clear ( );
            StatusNoteMessage.Clear ( );
            TitleMessage.Clear ( );
        }

        #endregion
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Common/Entities/User.cs $
 * 
 * 16    2/08/10 5:04p Camerons
 * 
 * 15    1/25/10 1:17p Camerons
 * 
 * 14    1/12/10 12:02p Camerons
 * 
 * 13    26/11/09 10:35a Camerons
 * 
 * 12    17/11/09 6:46p Camerons
 * 
 * 11    29/09/09 3:52p Camerons
 * Changes involving AllUsers.aspx and SPGroups. AllUsers page now
 * displays the recoreds from the SPGroups instead of Kiiro user list.
 * Allows for security to be managed for the SharePoint admin screens. 
 * 
 * 10    29/09/09 1:22p Camerons
 * Implementing User.SPUserId
 * 
 * 9     28/09/09 3:38p Camerons
 * 
 * 8     23/09/09 4:46p Camerons
 * Fixed some old code dealing with User.ManageProjects
 * 
 * 7     22/09/09 6:03p Camerons
 * Code changes to support creating user on demand - version 1.1.x.x
 * 
 * 6     17/08/09 10:38a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/